﻿#ifndef QUIWIDGET_H
#define QUIWIDGET_H

#include <QtCore>
#include <QtGui>
#include <QtWidgets>

class QUIWidget : public QDialog

{
    Q_OBJECT
    Q_ENUMS(Style)
    Q_PROPERTY(QString title READ getTitle WRITE setTitle)
    Q_PROPERTY(Qt::Alignment alignment READ getAlignment WRITE setAlignment)

public:
    //将部分对象作为枚举值暴露给外部
    enum Widget
    {
        Lab_Ico = 0,        //左上角图标
        BtnMenu = 1,        //下拉菜单按钮
        BtnMenu_Min = 2,    //最小化按钮
        BtnMenu_Max = 3,    //最大化按钮
        BtnMenu_Normal = 4, //还原按钮
        BtnMenu_Close = 5   //关闭按钮
    };

    //    样式枚举
    enum Style
    {
        Style_Silvery = 0,    //银色样式
        Style_Blue = 1,       //蓝色样式
        Style_LightBlue = 2,  //淡蓝色样式
        Style_DarkBlue = 3,   //深蓝色样式
        Style_Gray = 4,       //灰色样式
        Style_LightGray = 5,  //浅灰色样式
        Style_DarkGray = 6,   //深灰色样式
        Style_Black = 7,      //黑色样式
        Style_LightBlack = 8, //浅黑色样式
        Style_DarkBlack = 9,  //深黑色样式
        Style_PSBlack = 10,   //PS黑色样式
        Style_FlatBlack = 11, //黑色扁平样式
        Style_FlatWhite = 12  //白色扁平样式
    };

    //全局静态方法集合开始--------------------------------
public:
    //桌面宽度高度
    static int deskWidth();
    static int deskHeight();

    //弹出消息框
    static void showMessageBoxInfo(const QString &info, const QString &title, int closeSec = 0);
    //弹出错误框
    static void showMessageBoxError(const QString &info, int closeSec = 0);
    //弹出询问框
    static int showMessageBoxQuestion(const QString &info);

    //弹出输入框
    static QString showInputBox(bool &ok, const QString &title, int type = 0, int closeSec = 0,
                                QString defaultValue = QString(), bool pwd = false);

    //全局静态方法集合结束--------------------------------

public:
    explicit QUIWidget(QWidget *parent = 0);
    ~QUIWidget();

protected:
    bool eventFilter(QObject *obj, QEvent *evt);

private:
    QVBoxLayout *verticalLayout1;
    QWidget *widgetMain;
    QVBoxLayout *verticalLayout2;
    QWidget *widgetTitle;
    QHBoxLayout *horizontalLayout4;
    QLabel *labIco;
    QLabel *labTitle;
    QWidget *widgetMenu;
    QHBoxLayout *horizontalLayout;
    QToolButton *btnMenu;
    QPushButton *btnMenu_Min;
    QPushButton *btnMenu_Max;
    QPushButton *btnMenu_Close;
    QWidget *widget;
    QVBoxLayout *verticalLayout3;

private:
    bool max;       //是否处于最大化状态
    QRect location; //鼠标移动窗体后的坐标位置

    QString title;           //标题
    Qt::Alignment alignment; //标题文本对齐
    bool minHide;            //最小化隐藏
    QWidget *mainWidget;     //主窗体对象

public:
    QLabel *getLabIco() const;
    QLabel *getLabTitle() const;
    QToolButton *getBtnMenu() const;
    QPushButton *getBtnMenuMin() const;
    QPushButton *getBtnMenuMax() const;
    QPushButton *getBtnMenuMClose() const;

    Style getStyle() const;
    QString getTitle() const;
    Qt::Alignment getAlignment() const;

    QSize sizeHint() const;
    QSize minimumSizeHint() const;

private slots:
    void initControl(); //初始化控件
    void initForm();    //初始化窗体
    //    void changeStyle();             //更换样式

private slots:
    void on_btnMenu_Min_clicked();
    void on_btnMenu_Max_clicked();
    void on_btnMenu_Close_clicked();

public Q_SLOTS:
    //设置部件图标
    void setIcon(QUIWidget::Widget widget, QChar str, quint32 size = 9);
    void setIconMain(QChar str, quint32 size = 9);
    //设置部件图片
    void setPixmap(QUIWidget::Widget widget, const QString &file, const QSize &size = QSize(16, 16));
    //设置部件是否可见
    void setVisible(QUIWidget::Widget widget, bool visible = true);
    //设置只有关闭按钮
    void setOnlyCloseBtn();

    //设置标题栏高度
    void setTitleHeight(int height);
    //设置按钮统一宽度
    void setBtnWidth(int width);

    //设置标题及文本样式
    void setTitle(const QString &title);
    void setAlignment(Qt::Alignment alignment);

    //设置最小化隐藏
    void setMinHide(bool minHide);

    //设置主窗体
    void setMainWidget(QWidget *mainWidget);

Q_SIGNALS:
    void changeStyle(const QString &qssFile);
    void closing();
};

//弹出信息框类
class QUIMessageBox : public QDialog
{
    Q_OBJECT

public:
    explicit QUIMessageBox(QWidget *parent = 0);
    ~QUIMessageBox();

    static QUIMessageBox *Instance()
    {
        static QMutex mutex;

        if (!self)
        {
            QMutexLocker locker(&mutex);

            if (!self)
            {
                self = new QUIMessageBox;
            }
        }

        return self;
    }

protected:
    void closeEvent(QCloseEvent *);
    bool eventFilter(QObject *obj, QEvent *evt);

private:
    static QUIMessageBox *self;

    QVBoxLayout *verticalLayout1;
    QWidget *widgetTitle;
    QHBoxLayout *horizontalLayout3;
    QLabel *labIco;
    QLabel *labTitle;
    QLabel *labTime;
    QWidget *widgetMenu;
    QHBoxLayout *horizontalLayout4;
    QPushButton *btnMenu_Close;
    QWidget *widgetMain;
    QVBoxLayout *verticalLayout2;
    QFrame *frame;
    QVBoxLayout *verticalLayout4;
    QHBoxLayout *horizontalLayout1;
    QLabel *labIcoMain;
    QSpacerItem *horizontalSpacer1;
    QLabel *labInfo;
    QHBoxLayout *horizontalLayout2;
    QSpacerItem *horizontalSpacer2;
    QPushButton *btnOk;
    QPushButton *btnCancel;

private:
    int closeSec;   //总显示时间
    int currentSec; //当前已显示时间

private slots:
    void initControl(); //初始化控件
    void initForm();    //初始化窗体
    void checkSec();    //校验倒计时

private slots:
    void on_btnOk_clicked();
    void on_btnMenu_Close_clicked();

public Q_SLOTS:
    void setIconMain(QChar str, quint32 size = 9);
    void setMessage(const QString &msg, const QString &title, int type, int closeSec = 0);
};

//弹出输入框类
class QUIInputBox : public QDialog
{
    Q_OBJECT

public:
    explicit QUIInputBox(QWidget *parent = 0);
    ~QUIInputBox();

    static QUIInputBox *Instance()
    {
        static QMutex mutex;

        if (!self)
        {
            QMutexLocker locker(&mutex);

            if (!self)
            {
                self = new QUIInputBox;
            }
        }

        return self;
    }

protected:
    void closeEvent(QCloseEvent *);
    bool eventFilter(QObject *obj, QEvent *evt);

private:
    static QUIInputBox *self;

    QVBoxLayout *verticalLayout1;
    QWidget *widgetTitle;
    QHBoxLayout *horizontalLayout1;
    QLabel *labIco;
    QLabel *labTitle;
    QLabel *labTime;
    QWidget *widgetMenu;
    QHBoxLayout *horizontalLayout2;
    QPushButton *btnMenu_Close;
    QWidget *widgetMain;
    QVBoxLayout *verticalLayout2;
    QFrame *frame;
    QVBoxLayout *verticalLayout3;
    QLabel *labInfo;
    QLineEdit *txtValue;
    QComboBox *cboxValue;
    QHBoxLayout *lay;
    QSpacerItem *horizontalSpacer;
    QPushButton *btnOk;
    QPushButton *btnCancel;

private:
    int closeSec;   //总显示时间
    int currentSec; //当前已显示时间
    QString value;  //当前值

private slots:
    void initControl(); //初始化控件
    void initForm();    //初始化窗体
    void checkSec();    //校验倒计时

private slots:
    void on_btnOk_clicked();
    void on_btnMenu_Close_clicked();

public:
    QString getValue() const;

public Q_SLOTS:
    void setIconMain(QChar str, quint32 size = 9);
    void setParameter(const QString &title, int type = 0, int closeSec = 0,
                      QString defaultValue = QString(), bool pwd = false);
};

//图形字体处理类
class IconHelper : public QObject
{
    Q_OBJECT

public:
    explicit IconHelper(QObject *parent = 0);
    static IconHelper *Instance()
    {
        static QMutex mutex;

        if (!self)
        {
            QMutexLocker locker(&mutex);

            if (!self)
            {
                self = new IconHelper;
            }
        }

        return self;
    }

    void setIcon(QLabel *lab, QChar c, quint32 size = 9);
    void setIcon(QAbstractButton *btn, QChar c, quint32 size = 9);
    QPixmap getPixmap(const QString &color, QChar c, quint32 size = 9,
                      quint32 pixWidth = 10, quint32 pixHeight = 10);

    //根据按钮获取该按钮对应的图标
    QPixmap getPixmap(QToolButton *btn, bool normal);

    //指定导航面板样式,不带图标
    static void setStyle(QWidget *widget, const QString &type = "left", int borderWidth = 3,
                         const QString &borderColor = "#029FEA",
                         const QString &normalBgColor = "#292F38",
                         const QString &darkBgColor = "#1D2025",
                         const QString &normalTextColor = "#54626F",
                         const QString &darkTextColor = "#FDFDFD");

    //指定导航面板样式,带图标和效果切换
    void setStyle(QWidget *widget, QList<QToolButton *> btns, QList<int> pixChar,
                  quint32 iconSize = 9, quint32 iconWidth = 10, quint32 iconHeight = 10,
                  const QString &type = "left", int borderWidth = 3,
                  const QString &borderColor = "#029FEA",
                  const QString &normalBgColor = "#292F38",
                  const QString &darkBgColor = "#1D2025",
                  const QString &normalTextColor = "#54626F",
                  const QString &darkTextColor = "#FDFDFD");

    //指定导航按钮样式,带图标和效果切换
    void setStyle(QFrame *frame, QList<QToolButton *> btns, QList<int> pixChar,
                  quint32 iconSize = 9, quint32 iconWidth = 10, quint32 iconHeight = 10,
                  const QString &normalBgColor = "#2FC5A2",
                  const QString &darkBgColor = "#3EA7E9",
                  const QString &normalTextColor = "#EEEEEE",
                  const QString &darkTextColor = "#FFFFFF");

protected:
    bool eventFilter(QObject *watched, QEvent *event);

private:
    static IconHelper *self;   //对象自身
    QFont iconFont;            //图形字体
    QList<QToolButton *> btns; //按钮队列
    QList<QPixmap> pixNormal;  //正常图片队列
    QList<QPixmap> pixDark;    //加深图片队列
};

//全局变量控制
class QUIConfig
{
public:
    //全局图标
    static QChar IconMain;   //标题栏左上角图标
    static QChar IconMenu;   //下拉菜单图标
    static QChar IconMin;    //最小化图标
    static QChar IconMax;    //最大化图标
    static QChar IconNormal; //还原图标
    static QChar IconClose;  //关闭图标

    static QString FontName;   //全局字体名称
    static int FontSize;       //全局字体大小
    static QString ConfigFile; //配置文件文件路径及名称

    //样式表颜色值
    static QString TextColor;        //文字颜色
    static QString PanelColor;       //面板颜色
    static QString BorderColor;      //边框颜色
    static QString NormalColorStart; //正常状态开始颜色
    static QString NormalColorEnd;   //正常状态结束颜色
    static QString DarkColorStart;   //加深状态开始颜色
    static QString DarkColorEnd;     //加深状态结束颜色
    static QString HighColor;        //高亮颜色
};

#endif // QUIWIDGET_H
